import { _decorator, AudioClip, BoxCollider2D, Button, Component, Contact2DType, Director, director, dragonBones, ERigidBody2DType, find, instantiate, Label, Node, ParticleSystem2D, PolygonCollider2D, Prefab, resources, RigidBody2D, Sprite, SpriteFrame, Tween, tween, UIOpacity, UITransform, v2, v3 } from 'cc';
import { tagEnum } from './Enum';
import { getDistance, isNearlyEqual, randomNum, trackTarget } from './utils/utils';
import { UserInfo } from './UserInfo';
import { AudioMgr } from './utils/AudioMgr';
import { Adsdk } from './utils/Adsdk';
const { ccclass, property } = _decorator;

@ccclass('DoubleGameMgr')
export class DoubleGameMgr extends Component {

    @property(Node)
    player:Node;
    @property(Node)
    player2:Node;
    @property(Prefab)
    leftBlockPre:Prefab;
    @property(Prefab)
    rightBlockPre:Prefab;
    @property(Prefab)
    minTipPre:Prefab;
    @property(Node)
    gameRoot:Node;
    @property(Node)
    failPanel:Node;
    @property(Node)
    minTipRoot:Node;
    @property(Node)
    gameRoot2:Node;
    @property(Node)
    canvasMask:Node;
    @property([SpriteFrame])
    blockImgArr: SpriteFrame[] = [];
    @property(Label)
    playerScoreLabel: Label;
    @property(Label)
    playerScoreLabel2: Label;
    @property(Node)
    pausePanel: Node;
    @property(AudioClip)
    scale_into: AudioClip;
    @property(AudioClip)
    scale_loop: AudioClip;

    static ins: DoubleGameMgr = null;
    isClickIngBg = false;
    isClickIngBg2 = false;
    jumpTime = 1;//跳跃蓄力时间
    jumpTime2 = 1;//跳跃蓄力时间
    isJumpIng=false;
    isJumpIng2=false;
    isGameIng=true;
    isJudgeDrop=false;
    isJudgeDrop2=false;
    nextBlock=null;
    nextBlock2=null;


    leftPlayerStartWorldPos=v3(182,286,0);
    rightPlayerStartWorldPos=v3(578,286,0);

    leftPlayerStartWorldPos2=v3(182,917,0);
    rightPlayerStartWorldPos2=v3(578,917,0);

    currStandBlock=null;
    currStandBlock2=null;
    isMoveGameRootIng=false;
    isMoveGameRootIng2=false;


    playerDirection = 0;//0玩家朝右，1玩家朝左
    playerDirection2 = 0;//0玩家2朝右，1玩家朝左


    private playerScore = 0;
    get PlayerScore() {
        return this.playerScore;
    }
    set PlayerScore(value) {
        this.playerScore = value;
        this.playerScoreLabel.string = "得分：" + value;
    }

    private playerScore2 = 0;
    get PlayerScore2() {
        return this.playerScore2;
    }
    set PlayerScore2(value) {
        this.playerScore2 = value;
        this.playerScoreLabel2.string = "得分：" + value;
    }

    protected onLoad(): void {
        DoubleGameMgr.ins = this;
    }

    start() {
         //p1更换皮肤
         switch (UserInfo.p1SkinSelect) {
            case 0:
                this.player.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "player"
                break;
            case 1:
                this.player.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "playerGold"
                break;
            case 2:
                this.player.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "huahuaGold"
                break;
            case 3:
                this.player.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "jiewu"
                break;
            case 4:
                this.player.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "gongzhuDark"
                break;
            case 5:
                this.player.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "hongpi"
                break;

            default:
                this.player.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "player"
                break;
        }
         //p2更换皮肤
         switch (UserInfo.p2SkinSelect) {
            case 0:
                this.player2.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "player"
                break;
            case 1:
                this.player2.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "playerGold"
                break;
            case 2:
                this.player2.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "huahuaGold"
                break;
            case 3:
                this.player2.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "jiewu"
                break;
            case 4:
                this.player2.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "gongzhuDark"
                break;
            case 5:
                this.player2.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "hongpi"
                break;

            default:
                this.player2.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "player"
                break;
        }
        this.node.getChildByName("bg").on(Node.EventType.TOUCH_START, this.startTouch,this);
        this.node.getChildByName("bg").on(Node.EventType.TOUCH_END, this.endTouch,this);
        this.node.getChildByName("bg").on(Node.EventType.TOUCH_CANCEL, this.endTouch,this);

        this.node.getChildByName("bg2").on(Node.EventType.TOUCH_START, this.startTouch2,this);
        this.node.getChildByName("bg2").on(Node.EventType.TOUCH_END, this.endTouch2,this);
        this.node.getChildByName("bg2").on(Node.EventType.TOUCH_CANCEL, this.endTouch2,this);

        this.player.getComponent(BoxCollider2D).on(Contact2DType.BEGIN_CONTACT,this.beginContact);
        this.player2.getComponent(BoxCollider2D).on(Contact2DType.BEGIN_CONTACT,this.beginContact2);
        this.currStandBlock=this.gameRoot.getChildByName("blockRoot").getChildByName("firstBlock");
        this.currStandBlock2=this.gameRoot2.getChildByName("blockRoot").getChildByName("firstBlock");

        this.PlayerScore = 0;
        this.PlayerScore2 = 0;

        let dragonBonesComp = this.player.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay);
        dragonBonesComp.playAnimation("land", 1);
        let dragonBonesComp2=this.player2.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay);
        dragonBonesComp2.playAnimation("land",1);
        dragonBonesComp.addEventListener(dragonBones.EventObject.COMPLETE, () => {
            dragonBonesComp.playAnimation("stand", 1);
        },this);
        dragonBonesComp2.addEventListener(dragonBones.EventObject.COMPLETE, () => {
            dragonBonesComp2.playAnimation("stand", 1);
            this.canvasMask.active=false;
        },this);

        // if (UserInfo.isAudioBgm) {
        //     AudioMgr.ins.play("audio/bgm")
        // }
        // else{
            AudioMgr.ins.stop();
        // }

        AudioMgr.ins.playOneShot("audio/start",0.8);

        this.nextBlock=this.player.parent.getChildByName("blockRoot").getChildByName("rightBlock");
        this.nextBlock2=this.player2.parent.getChildByName("blockRoot").getChildByName("rightBlock");


    }
    gameReSet(){
        this.isClickIngBg=false;
        this.isClickIngBg2=false;
        this.jumpTime=1;
        this.jumpTime2=1;
        this.isJumpIng=false;
        this.isJumpIng2=false;
        this.isMoveGameRootIng=false;
        this.isMoveGameRootIng2=false;
        this.isClickIngBgTempCount=0;
        this.isClickIngBgTempCount2=0;


        // this.playerDirection = 0;//0玩家朝右，1玩家朝左
        // this.playerDirection2 = 0;//0玩家2朝右，1玩家朝左
        // this.currStandBlock=this.gameRoot.getChildByName("blockRoot").getChildByName("firstBlock");
        // this.currStandBlock2=this.gameRoot2.getChildByName("blockRoot").getChildByName("firstBlock");
        // this.PlayerScore=0;
    }


    beginContact(self, other) {
        director.once(Director.EVENT_AFTER_PHYSICS, () => {

            if (other.tag == tagEnum.block) {
                if (other.node == DoubleGameMgr.ins.currStandBlock) {
                    return;
                }
               
                DoubleGameMgr.ins.player.getComponent(RigidBody2D).linearVelocity = v2(0, 0);
                DoubleGameMgr.ins.player.getComponent(RigidBody2D).type = ERigidBody2DType.Kinematic;
                other.node.getComponent(PolygonCollider2D).enabled=false;
                other.node.getComponent(RigidBody2D).enabled=false;
                DoubleGameMgr.ins.currStandBlock = other.node;
                DoubleGameMgr.ins.isJudgeDrop=false;

                AudioMgr.ins.playOneShot("audio/playerJump",0.8);
                let centerPos = other.node.getChildByName("centerPoint").getWorldPosition();
                let playerCenterPos = DoubleGameMgr.ins.player.getChildByName("centerPoint").getWorldPosition();
                if (getDistance(centerPos.x, centerPos.y, playerCenterPos.x, playerCenterPos.y) < 15) {
                    console.log("完美跳跃")
                    AudioMgr.ins.playOneShot("audio/perfect",0.7);
                    let dragonBonesComp = DoubleGameMgr.ins.player.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay);
                   let ani= dragonBonesComp.playAnimation("celebrate", 1);
                   ani.timeScale=1.5
                  
                    DoubleGameMgr.ins.PlayerScore += 2;

                    console.log(other.node.getWorldPosition(),"other.node.getWorldPosition()")
                        let minTipPos = DoubleGameMgr.ins.minTipRoot.getComponent(UITransform).convertToNodeSpaceAR(other.node.getWorldPosition());
                        DoubleGameMgr.ins.createMinStr(minTipPos, "完美跳跃");
                   
                }
                else {
                    DoubleGameMgr.ins.PlayerScore += 1;
                }

                let randDir=randomNum(0,1)
                if (randDir==0) {
                    DoubleGameMgr.ins.playerDirection=1
                  
                    DoubleGameMgr.ins.player.scale=v3(-0.7,0.7,0.7)

                }
                else{
                    DoubleGameMgr.ins.playerDirection=0
                  
                    DoubleGameMgr.ins.player.scale=v3(0.7,0.7,0.7)
                }

                setTimeout(() => {
                    if (!DoubleGameMgr.ins.isMoveGameRootIng) {
                    DoubleGameMgr.ins.moveGameRoot();
                        
                    }
                }, 1000)

            }

            if (other.tag==tagEnum.dieLine) {
               
                DoubleGameMgr.ins.gameOver(0);
            }
        })
    }

    beginContact2(self, other) {
        director.once(Director.EVENT_AFTER_PHYSICS, () => {

            if (other.tag == tagEnum.block) {
                if (other.node == DoubleGameMgr.ins.currStandBlock2) {
                    return;
                }
               
                DoubleGameMgr.ins.player2.getComponent(RigidBody2D).linearVelocity = v2(0, 0);
                DoubleGameMgr.ins.player2.getComponent(RigidBody2D).type = ERigidBody2DType.Kinematic;
                other.node.getComponent(PolygonCollider2D).enabled=false;
                other.node.getComponent(RigidBody2D).enabled=false;
                DoubleGameMgr.ins.currStandBlock2 = other.node;
                DoubleGameMgr.ins.isJudgeDrop2=false;

                let centerPos = other.node.getChildByName("centerPoint").getWorldPosition();
                let playerCenterPos = DoubleGameMgr.ins.player2.getChildByName("centerPoint").getWorldPosition();
                if (getDistance(centerPos.x, centerPos.y, playerCenterPos.x, playerCenterPos.y) < 15) {
                    console.log("完美跳跃")
                    AudioMgr.ins.playOneShot("audio/perfect",0.7);

                    let dragonBonesComp = DoubleGameMgr.ins.player2.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay);
                    let ani = dragonBonesComp.playAnimation("celebrate", 1);
                    ani.timeScale = 1.5
                    DoubleGameMgr.ins.PlayerScore2 += 2;

                    let minTipPos = DoubleGameMgr.ins.minTipRoot.getComponent(UITransform).convertToNodeSpaceAR(other.node.getWorldPosition());
                    DoubleGameMgr.ins.createMinStr(minTipPos, "完美跳跃");

                }
                else {
                    DoubleGameMgr.ins.PlayerScore2 += 1;
                }

                let randDir=randomNum(0,1)
                if (randDir==0) {
                    DoubleGameMgr.ins.playerDirection2=1
                  
                    DoubleGameMgr.ins.player2.scale=v3(-0.7,0.7,0.7)

                }
                else{
                    DoubleGameMgr.ins.playerDirection2=0
                  
                    DoubleGameMgr.ins.player2.scale=v3(0.7,0.7,0.7)
                }

                setTimeout(() => {
                    if (!DoubleGameMgr.ins.isMoveGameRootIng2) {
                    DoubleGameMgr.ins.moveGameRoot2();
                        
                    }
                }, 1000)

            }

            if (other.tag==tagEnum.dieLine) {
              
                DoubleGameMgr.ins.gameOver(1);
            }
        })
    }

    isPlayScaleAudio;
    isPlayScaleAudio2;

    startTouch() {
        if(!this.isJumpIng){
            this.isClickIngBg=true;

            this.isPlayScaleAudio=true;
            AudioMgr.ins.audioSource2.clip=this.scale_into;
            AudioMgr.ins.audioSource2.loop=false;
            AudioMgr.ins.audioSource2.volume=0.8;
            AudioMgr.ins.audioSource2.play();
            // setTimeout(() => {
            //     if (this.isPlayScaleAudio) {
            //         AudioMgr.ins.audioSource2.clip=this.scale_loop;
            //         AudioMgr.ins.audioSource2.loop=true;
            //         AudioMgr.ins.audioSource2.volume=0.8;
            //         AudioMgr.ins.audioSource2.play();
            //     }
            // }, 2000);

            if (this.currStandBlock) {
                tween(this.currStandBlock.getChildByName("sp"))
                .tag(42)
                .by(2.5,{scale:v3(0,-0.4,0),position:v3(0,-15,0)})
                .start()

                tween(this.player.getChildByName("sp"))
                .tag(42)
                .by(2.5,{position:v3(0,-35,0),scale:v3(0,-0.2,0)})
                .start()
            }
            this.player.getChildByName("jumpParticle2D").getComponent(ParticleSystem2D).resetSystem();

        }
    }
    startTouch2() {
        if(!this.isJumpIng2){
            this.isClickIngBg2=true;

            this.isPlayScaleAudio2=true;
            AudioMgr.ins.audioSource3.clip=this.scale_into;
            AudioMgr.ins.audioSource3.loop=false;
            AudioMgr.ins.audioSource3.volume=0.8;
            AudioMgr.ins.audioSource3.play();
            // setTimeout(() => {
            //     if (this.isPlayScaleAudio2) {
            //         AudioMgr.ins.audioSource3.clip=this.scale_loop;
            //         AudioMgr.ins.audioSource3.loop=true;
            //         AudioMgr.ins.audioSource3.volume=0.8;
            //         AudioMgr.ins.audioSource3.play();
            //     }
            // }, 2000);

            if (this.currStandBlock2) {
                tween(this.currStandBlock2.getChildByName("sp"))
                .tag(43)
                .by(2.5,{scale:v3(0,-0.4,0),position:v3(0,-15,0)})
                .start()

                tween(this.player2.getChildByName("sp"))
                .tag(43)
                .by(2.5,{position:v3(0,-35,0),scale:v3(0,-0.2,0)})
                .start()
            }
            this.player2.getChildByName("jumpParticle2D").getComponent(ParticleSystem2D).resetSystem();

        }
    }
    endTouch() {
        if (!this.isJumpIng) {
            this.isJumpIng = true;
            this.isPlayScaleAudio=false;
            AudioMgr.ins.audioSource2.stop();

            if (this.currStandBlock) {
            Tween.stopAllByTag(42);
            this.currStandBlock.getChildByName("sp").scale=v3(1,1,1);
            this.currStandBlock.getChildByName("sp").setPosition(0,0,0);
            this.player.getChildByName("sp").setPosition(0,0,0);
            this.player.getChildByName("sp").scale=v3(1,1,1);
            
            }
            this.player.getChildByName("jumpParticle2D").getComponent(ParticleSystem2D).stopSystem();

            // this.player.getComponent(RigidBody2D).linearVelocity = v2(4, 9);
            if (this.playerDirection == 0) {
                this.player.getComponent(RigidBody2D).linearVelocity = v2(4 * this.jumpTime, 9 * this.jumpTime);
            }
            else {
                this.player.getComponent(RigidBody2D).linearVelocity = v2(-4 * this.jumpTime, 9 * this.jumpTime);
            }
            // if (this.playerDirection==0) {
            //     this.player.getComponent(RigidBody2D).linearVelocity = v2(6, 14);
            // }
            // else {
            //     this.player.getComponent(RigidBody2D).linearVelocity = v2(-6, 14);
            // }
            // this.player.getComponent(RigidBody2D).linearVelocity = v2(10, 24);
            this.player.getComponent(RigidBody2D).type = ERigidBody2DType.Dynamic;

            this.isClickIngBg = false;
            this.jumpTime = 1;
            this.isClickIngBgTempCount = 0;
            setTimeout(() => {
                this.isJudgeDrop = true;

            }, 500)
        }

    }
    endTouch2() {
        if (!this.isJumpIng2) {
            this.isJumpIng2 = true;
            this.isPlayScaleAudio2=false;
            AudioMgr.ins.audioSource3.stop();

            if (this.currStandBlock2) {
                Tween.stopAllByTag(43);
                this.currStandBlock2.getChildByName("sp").scale = v3(1, 1, 1);
                this.currStandBlock2.getChildByName("sp").setPosition(0, 0, 0);
                this.player2.getChildByName("sp").setPosition(0, 0, 0);
                this.player2.getChildByName("sp").scale = v3(1, 1, 1);
            }
            this.player2.getChildByName("jumpParticle2D").getComponent(ParticleSystem2D).stopSystem();

            if (this.playerDirection2 == 0) {
                this.player2.getComponent(RigidBody2D).linearVelocity = v2(4 * this.jumpTime2, 9 * this.jumpTime2);
            }
            else {
                this.player2.getComponent(RigidBody2D).linearVelocity = v2(-4 * this.jumpTime2, 9 * this.jumpTime2);
            }

            this.player2.getComponent(RigidBody2D).type = ERigidBody2DType.Dynamic;

            this.isClickIngBg2 = false;
            this.jumpTime2 = 1;
            this.isClickIngBgTempCount2 = 0;
            setTimeout(() => {
                this.isJudgeDrop2 = true;

            }, 500)
        }

    }

    //创建块
    createOneBloack() {
        if (this.playerDirection == 0) {//如果朝向是右边
            let pre = instantiate(this.rightBlockPre);
            pre.setParent(this.gameRoot.getChildByName("blockRoot"));

            let randSca=randomNum(0,1)
            if (randSca==0) {
                pre.scale=v3(1,1,1);
            }
            else{
                pre.scale=v3(0.9,0.9,0.9);
            }

            if (this.PlayerScore < 20) {
                pre.getChildByName("sp").getComponent(Sprite).spriteFrame = this.blockImgArr[0];
            }
            else if (this.PlayerScore >= 20 && this.PlayerScore < 40) {
                pre.getChildByName("sp").getComponent(Sprite).spriteFrame = this.blockImgArr[1];
            }
            else if (this.PlayerScore >= 40 && this.PlayerScore < 60) {
                pre.getChildByName("sp").getComponent(Sprite).spriteFrame = this.blockImgArr[2];
            }
            else {
                pre.getChildByName("sp").getComponent(Sprite).spriteFrame = this.blockImgArr[3];
            }
            
            let rand = randomNum(0, 1)
            if (rand == 0) {
                pre.setWorldPosition(381, 435, 0);
            }
            else {
                pre.setWorldPosition(326, 350, 0);
            }
            pre.getComponent(UIOpacity).opacity = 0;
            tween(pre.getComponent(UIOpacity))
                .to(0.7, { opacity: 255 })
                .start()
                this.nextBlock=pre;

        }
        else {
            let pre = instantiate(this.leftBlockPre);
            pre.setParent(this.gameRoot.getChildByName("blockRoot"));

            let randSca=randomNum(0,1)
            if (randSca==0) {
                pre.scale=v3(1,1,1);
            }
            else{
                pre.scale=v3(0.9,0.9,0.9);
            }

            if (this.PlayerScore < 20) {
                pre.getChildByName("sp").getComponent(Sprite).spriteFrame = this.blockImgArr[0];
            }
            else if (this.PlayerScore >= 20 && this.PlayerScore < 40) {
                pre.getChildByName("sp").getComponent(Sprite).spriteFrame = this.blockImgArr[1];
            }
            else if (this.PlayerScore >= 40 && this.PlayerScore < 60) {
                pre.getChildByName("sp").getComponent(Sprite).spriteFrame = this.blockImgArr[2];
            }
            else {
                pre.getChildByName("sp").getComponent(Sprite).spriteFrame = this.blockImgArr[3];
            }
            

            let rand = randomNum(0, 1)
            if (rand == 0) {
                pre.setWorldPosition(381, 435, 0);
            }
            else {
                pre.setWorldPosition(430, 350, 0);
            }
            pre.getComponent(UIOpacity).opacity=0;
            tween(pre.getComponent(UIOpacity))
            .to(0.7,{opacity:255})
            .start()
            this.nextBlock=pre;

        }
    }
     //创建块
     createOneBloack2() {
        if (this.playerDirection2 == 0) {//如果朝向是右边
            let pre = instantiate(this.rightBlockPre);
            pre.setParent(this.gameRoot2.getChildByName("blockRoot"));

            let randSca=randomNum(0,1)
            if (randSca==0) {
                pre.scale=v3(1,1,1);
            }
            else{
                pre.scale=v3(0.9,0.9,0.9);
            }
            
            if (this.PlayerScore2 < 30) {
                pre.getChildByName("sp").getComponent(Sprite).spriteFrame = this.blockImgArr[0];
            }
            else if (this.PlayerScore2 >= 30 && this.PlayerScore2 < 60) {
                pre.getChildByName("sp").getComponent(Sprite).spriteFrame = this.blockImgArr[1];
            }
            else if (this.PlayerScore2 >= 60 && this.PlayerScore2 < 90) {
                pre.getChildByName("sp").getComponent(Sprite).spriteFrame = this.blockImgArr[2];
            }
            else {
                pre.getChildByName("sp").getComponent(Sprite).spriteFrame = this.blockImgArr[3];
            }

            let rand = randomNum(0, 1)
            if (rand == 0) {
                pre.setWorldPosition(381, 1059, 0);
            }
            else {
                pre.setWorldPosition(324, 989, 0);

            }
            pre.getComponent(UIOpacity).opacity=0;
            tween(pre.getComponent(UIOpacity))
            .to(0.7,{opacity:255})
            .start()
            this.nextBlock2=pre;


        }
         else {
             let pre = instantiate(this.leftBlockPre);
             pre.setParent(this.gameRoot2.getChildByName("blockRoot"));
             
            let randSca=randomNum(0,1)
            if (randSca==0) {
                pre.scale=v3(1,1,1);
            }
            else{
                pre.scale=v3(0.9,0.9,0.9);
            }

             if (this.PlayerScore2 < 30) {
                pre.getChildByName("sp").getComponent(Sprite).spriteFrame = this.blockImgArr[0];
            }
            else if (this.PlayerScore2 >= 30 && this.PlayerScore2 < 60) {
                pre.getChildByName("sp").getComponent(Sprite).spriteFrame = this.blockImgArr[1];
            }
            else if (this.PlayerScore2 >= 60 && this.PlayerScore2 < 90) {
                pre.getChildByName("sp").getComponent(Sprite).spriteFrame = this.blockImgArr[2];
            }
            else {
                pre.getChildByName("sp").getComponent(Sprite).spriteFrame = this.blockImgArr[3];
            }

             let rand = randomNum(0, 1)
             if (rand == 0) {
                 pre.setWorldPosition(381, 1059, 0);
             }
             else {
                 pre.setWorldPosition(430, 989, 0);
             }
             pre.getComponent(UIOpacity).opacity = 0;
             tween(pre.getComponent(UIOpacity))
                 .to(0.7, { opacity: 255 })
                 .start()
            this.nextBlock2=pre;

         }
     }

    moveGameRoot() {
        for (let i = 0; i < this.gameRoot.getChildByName("blockRoot").children.length; i++) {
            let block=this.gameRoot.getChildByName("blockRoot").children[i];
            if (block.getWorldPosition().x<-500||block.getWorldPosition().x>2000) {
                console.log("块销毁")
                block.destroy();
            }
            if (block.getWorldPosition().y<-500||block.getWorldPosition().y>2500) {
                console.log("块销毁")
                block.destroy();
            }
            
        }
       
        this.isMoveGameRootIng = true;
        if (this.playerDirection == 0) {
            let currBlockPos = this.currStandBlock.getWorldPosition();
            let dir = v2(currBlockPos.x - this.leftPlayerStartWorldPos.x, currBlockPos.y - this.leftPlayerStartWorldPos.y);
            let pos = this.gameRoot.getPosition();

            tween(this.gameRoot)
                .to(1, { position: v3(pos.x - dir.x, pos.y - dir.y, 0) })
                .call(() => {
                    this.createOneBloack();
                    this.isMoveGameRootIng = false;
                    this.isJumpIng = false;

                })
                .start()
        }
        else {
            let currBlockPos = this.currStandBlock.getWorldPosition();
            let dir = v2(currBlockPos.x - this.rightPlayerStartWorldPos.x, currBlockPos.y - this.rightPlayerStartWorldPos.y);
            let pos = this.gameRoot.getPosition();

            tween(this.gameRoot)
                .to(1, { position: v3(pos.x - dir.x, pos.y - dir.y, 0) })
                .call(() => {
                    this.createOneBloack();
                    this.isMoveGameRootIng = false;
                    this.isJumpIng = false;

                })
                .start()
        }

    }

    moveGameRoot2() {
        for (let i = 0; i < this.gameRoot2.getChildByName("blockRoot").children.length; i++) {
            let block=this.gameRoot2.getChildByName("blockRoot").children[i];
            if (block.getWorldPosition().y<600) {
                console.log("块销毁")
                block.destroy();
            }
            
        }
       
        this.isMoveGameRootIng2 = true;
        if (this.playerDirection2 == 0) {
            let currBlockPos = this.currStandBlock2.getWorldPosition();
            let dir = v2(currBlockPos.x - this.leftPlayerStartWorldPos2.x, currBlockPos.y - this.leftPlayerStartWorldPos2.y);
            let pos = this.gameRoot2.getPosition();

            tween(this.gameRoot2)
                .to(1, { position: v3(pos.x - dir.x, pos.y - dir.y, 0) })
                .call(() => {
                    this.createOneBloack2();
                    this.isMoveGameRootIng2 = false;
                    this.isJumpIng2 = false;

                })
                .start()
        }
        else {
            let currBlockPos = this.currStandBlock2.getWorldPosition();
            let dir = v2(currBlockPos.x - this.rightPlayerStartWorldPos2.x, currBlockPos.y - this.rightPlayerStartWorldPos2.y);
            let pos = this.gameRoot2.getPosition();

            tween(this.gameRoot2)
                .to(1, { position: v3(pos.x - dir.x, pos.y - dir.y, 0) })
                .call(() => {
                    this.createOneBloack2();
                    this.isMoveGameRootIng2 = false;
                    this.isJumpIng2 = false;

                })
                .start()
        }

    }


    isClickIngBgTempCount = 0;
    isClickIngBgTempCount2 = 0;
    tempPlayerPos=v3();
    tempPlayerPosPre=v3();
    tempBlockPos=v3();
    tempPlayerPos2=v3();
    tempPlayerPosPre2=v3();
    tempBlockPos2=v3();
    update(deltaTime: number) {
        if (this.isGameIng) {
            if (this.isClickIngBg) {
                this.isClickIngBgTempCount += deltaTime;
    
                if (this.isClickIngBgTempCount > 0.2) {
                    this.isClickIngBgTempCount = 0;
                    if (this.jumpTime<2) {
                    this.jumpTime += 0.2;
                        
                    }
    
                }
    
            }
    
            if (this.isClickIngBg2) {
                this.isClickIngBgTempCount2 += deltaTime;
    
                if (this.isClickIngBgTempCount2 > 0.2) {
                    this.isClickIngBgTempCount2 = 0;
                    if (this.jumpTime2<2) {
                    this.jumpTime2 += 0.2;
                        
                    }
    
                }
    
            }
    
            if (this.isJudgeDrop) {
                this.player.getWorldPosition(this.tempPlayerPos);
                if (this.tempPlayerPosPre!=v3(0,0,0)) {
                    if (this.tempPlayerPos.y<this.tempPlayerPosPre.y) {
                        //下落

                        if (this.nextBlock) {
                            if (this.tempPlayerPos.y < this.nextBlock.getWorldPosition(this.tempBlockPos).y - 20) {
                                this.isJudgeDrop = false;
                                this.isJudgeDrop2 = false;
                                this.gameOver(0);
                            }
                        }

                    }
                }

                this.player.getWorldPosition(this.tempPlayerPosPre);
            }

            if (this.isJudgeDrop2) {
                this.player2.getWorldPosition(this.tempPlayerPos2);
                if (this.tempPlayerPosPre2!=v3(0,0,0)) {
                    if (this.tempPlayerPos2.y<this.tempPlayerPosPre2.y) {
                        //下落
    
                        console.log("下落")
                        if (this.nextBlock2) {
                            if (this.tempPlayerPos2.y<this.nextBlock2.getWorldPosition(this.tempBlockPos2).y-20) {
                                this.isJudgeDrop2=false;
                                this.isJudgeDrop=false;
                                this.gameOver(1);
                            }
                        }
                       
                    }
                }
           
                this.player2.getWorldPosition(this.tempPlayerPosPre2);
            }
    
        }
       

    }

    gameOver(playerDieIndex) {
        if (this.isGameIng) {
            if (playerDieIndex == 0) {
                DoubleGameMgr.ins.player.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).playAnimation("die", 1);
                DoubleGameMgr.ins.player.getComponent(RigidBody2D).linearVelocity = v2(0, 0);
                DoubleGameMgr.ins.player.getComponent(RigidBody2D).type = ERigidBody2DType.Kinematic;
            }
            else {
                DoubleGameMgr.ins.player2.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).playAnimation("die", 1);
                DoubleGameMgr.ins.player2.getComponent(RigidBody2D).linearVelocity = v2(0, 0);
                DoubleGameMgr.ins.player2.getComponent(RigidBody2D).type = ERigidBody2DType.Kinematic;
            }


            AudioMgr.ins.playOneShot("audio/drop", 0.7);

            this.isGameIng = false;
            setTimeout(() => {
                AudioMgr.ins.playOneShot("audio/lose", 0.8);
                this.failPanel.active = true;
                this.failPanel.getChildByName("daily_smallframe").getChildByName("scoreLabel").getComponent(Label).string = this.PlayerScore + "";
                this.failPanel.getChildByName("daily_smallframe").getChildByName("scoreLabel2").getComponent(Label).string = this.playerScore2 + "";
                this.failPanel.getChildByName("daily_smallframe").getChildByName("totalScoreLabel").getComponent(Label).string = `${this.playerScore2 + this.playerScore}`;

                if (localStorage.getItem("doubleMaxScore")) {
                    this.failPanel.getChildByName("samll_form").getChildByName("preTotalScoreLabel").getComponent(Label).string = JSON.parse(localStorage.getItem("doubleMaxScore"));
                }

                if (localStorage.getItem("doubleMaxScore")) {
                    if (JSON.parse(localStorage.getItem("doubleMaxScore")) < this.playerScore2 + this.playerScore) {
                        localStorage.setItem("doubleMaxScore", JSON.stringify(this.playerScore2 + this.playerScore));
                    }
                }
                else {
                    localStorage.setItem("doubleMaxScore", JSON.stringify(this.playerScore2 + this.playerScore));
                }

                if (playerDieIndex == 1) {
                    this.failPanel.getChildByName("title").getComponent(Label).string = "p1胜利"
                }
                else {
                    this.failPanel.getChildByName("title").getComponent(Label).string = "p2胜利"

                }
            }, 1000);

        }

    }

    onclickReviveBtn() {
        Adsdk.instance.showVideo(() => {
            this.failPanel.active = false;
            let pos1 = this.currStandBlock.getPosition();
            let pos2 = this.currStandBlock2.getPosition();
            this.player.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).playAnimation("stand",0);
            this.player2.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).playAnimation("stand",0);
            this.player.getComponent(RigidBody2D).linearVelocity = v2(0, 0);
            this.player.getComponent(RigidBody2D).type = ERigidBody2DType.Kinematic;
            this.player2.getComponent(RigidBody2D).linearVelocity = v2(0, 0);
            this.player2.getComponent(RigidBody2D).type = ERigidBody2DType.Kinematic;
            this.player.setPosition(v3(pos1.x, pos1.y+60, 0));
            this.player2.setPosition(v3(pos2.x, pos2.y+60, 0));
            this.isGameIng=true;
            this.gameReSet();
        },()=>{

        })

    }

    onclickHomeBtn(){
        AudioMgr.ins.playOneShot("audio/click",0.8);


        director.loadScene("start")
        
    }

    onclickRestartBtn(){
        AudioMgr.ins.playOneShot("audio/click",0.8);

        director.loadScene("doubleGame")
        
    }
    onclickContinueBtn(){
        AudioMgr.ins.playOneShot("audio/click",0.8);

        this.pausePanel.active=false;

    }
    onclickPauseBtn() {
        Adsdk.instance.showInterstitialAd();

        AudioMgr.ins.playOneShot("audio/click", 0.8);

        this.pausePanel.active = true;;
        this.renewAudioUI();
    }

    createMinStr(pos,str){
        let pre=instantiate(this.minTipPre);
        pre.setParent(this.minTipRoot);
        pre.setPosition(pos.x,pos.y);
        pre.getChildByName("Label").getComponent(Label).string=str;
        tween(pre)
            .by(0.7, { position: v3(0, 80, 0) })
            .call(() => {
                pre.destroy();
            })
            .start()
    }


    onclickSetPanelaudioEffectBtn() {
        if (UserInfo.isAudioEffect) {
            UserInfo.isAudioEffect = false;
        }
        else {
            UserInfo.isAudioEffect = true;
        }

        this.renewAudioUI();
    }
    onclickSetPanelmusicBtn() {
        if (UserInfo.isAudioBgm) {
            UserInfo.isAudioBgm = false;
            AudioMgr.ins.stop();
        }
        else {
            UserInfo.isAudioBgm = true;
            AudioMgr.ins.play("audio/bgm");
        }
        this.renewAudioUI();

    }


    renewAudioUI() {
        if (UserInfo.isAudioBgm) {
            resources.load("audioImg/musicOn/spriteFrame", SpriteFrame, (err, res) => {
                this.pausePanel.getChildByName("bgmBtn").getChildByName("sp").getComponent(Sprite).spriteFrame = res;

            })
        }
        else {
            resources.load("audioImg/musicOff/spriteFrame", SpriteFrame, (err, res) => {
                this.pausePanel.getChildByName("bgmBtn").getChildByName("sp").getComponent(Sprite).spriteFrame = res;

            })
        }

        if (UserInfo.isAudioEffect) {
            resources.load("audioImg/effectOn/spriteFrame", SpriteFrame, (err, res) => {
                this.pausePanel.getChildByName("effectBtn").getChildByName("sp").getComponent(Sprite).spriteFrame = res;

            })
        }
        else {
            resources.load("audioImg/effectOff/spriteFrame", SpriteFrame, (err, res) => {
                this.pausePanel.getChildByName("effectBtn").getChildByName("sp").getComponent(Sprite).spriteFrame = res;

            })
        }
    }


}


